package com.domain.oa.controller;

import com.domain.oa.entity.Menu;
import com.domain.oa.service.impl.MenuServiceImpl;
import com.domain.oa.utils.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Menu)表控制层
 *
 * @author makejava
 * @since 2023-02-20 15:07:36
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Resource
    private MenuServiceImpl dataService;

    /**
     * 分页获取数据,如果请求参数中的tree为1,则返回二叉树数据
     * @param pageCurrent
     * @param pageSize
     * @return ResultVo
     */
    @GetMapping("/page")
    public ResultVo getPage(@RequestParam Integer pageCurrent,
                            @RequestParam Integer pageSize,
                            @RequestParam(defaultValue = "0") String Tree) {
        if (Tree == "0") {
            return dataService.getDataByPage(pageCurrent, pageSize);
        }else {
            return dataService.getMenuByPageToTree(pageCurrent, pageSize);
        }

    }

    /**
     * 修改数据
     * @param data
     * @return ResultVo
     */
    @PostMapping
    public ResultVo editData(@RequestBody Menu data) {
        return dataService.editData(data);
    }

    /**
     * 添加数据
     * @param data
     * @return
     */
    @PutMapping
    public ResultVo putData(@RequestBody Menu data) {
        return dataService.editData(data);
    }

    /**
     * 删除数据
     * @param id
     * @return ResultVo
     */
    @DeleteMapping("/{id}")
    public ResultVo deleteData(@PathVariable Integer id) {
        return dataService.deleteDataById(id);
    }


    @GetMapping("/all")
    public ResultVo getAllData() {
        return dataService.getAllMenu();
    }

    @GetMapping("/{roleId}")
    public ResultVo getMenuByRoleId(@PathVariable Integer roleId) {
        return dataService.getMenuByRoleId(roleId);
    }
}

